package net.mikoo.seals.boss.biz.services.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;

import net.mikoo.seals.boss.biz.mapper.boss.AgentMapper;
import net.mikoo.seals.boss.biz.mapper.boss.PartyMapper;
import net.mikoo.seals.boss.biz.mapper.boss.PersonMapper;
import net.mikoo.seals.boss.biz.mapper.boss.UserLoginMapper;
import net.mikoo.seals.boss.biz.services.AgentServices;
import net.mikoo.seals.boss.common.BaseServices;
import net.mikoo.seals.boss.common.PageModel;
import net.mikoo.seals.boss.common.enums.PartyStatusEnum;
import net.mikoo.seals.boss.common.enums.PartyTypeEnum;
import net.mikoo.seals.boss.common.exception.ParametersException;
import net.mikoo.seals.boss.common.utils.MD5Util;
import net.mikoo.seals.boss.model.Agent;
import net.mikoo.seals.boss.model.AgentQuery;
import net.mikoo.seals.boss.model.Party;
import net.mikoo.seals.boss.model.Person;
import net.mikoo.seals.boss.model.UserLogin;

@Service
public class AgentServicesImpl extends BaseServices implements AgentServices {
	
	Logger logger = LoggerFactory.getLogger(AgentServicesImpl.class);

	@Autowired
	private AgentMapper agentMapper;
	
	@Autowired
	private PartyMapper partyMapper;
	
	@Autowired
	private PersonMapper personMapper;
	
	@Autowired
	private UserLoginMapper userLoginMapper;
	
	@Override
	public void save(Agent agent) {
		
		if(agent == null){
			throw new ParametersException("agent is null!");
		}
		
		if(StringUtils.isBlank(agent.getCompanyName())) {
			throw new ParametersException("请输入公司名称");
		}
		
		if(StringUtils.isBlank(agent.getUserLoginId())) {
			throw new ParametersException("请输入代理商账户");
		}
		
		if(StringUtils.isBlank(agent.getPassword())) {
			throw new ParametersException("请输入代理商密码");
		}
		
		this.bossTransactionTemplate.execute(new TransactionCallbackWithoutResult() {
			
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				try {
					
					
					
					
					Party party = new Party();
					party.setPartyType(PartyTypeEnum.AGENT.code());
					party.setUserLoginId(agent.getUserLoginId());
					party.setCreatedTime(new Date());
					party.setPartyStatus(PartyStatusEnum.NORMAL.code());
					party.setCreatedBy(0L);
					partyMapper.insertBackPrimaryKey(party);
					
					UserLogin login = new UserLogin();
					login.setPartyId(party.getPartyId());
					login.setUserLoginId(agent.getUserLoginId());
					login.setCurrentPassword(MD5Util.Md5(agent.getPassword()));
					login.setIsSystem("Y");
					login.setEnabled("Y");
					login.setRegisterIp("127.0.0.1");
					login.setRegisterTime(new Date());
					login.setCreateTime(new Date());
					login.setRequirePasswordChange("N");
					userLoginMapper.insert(login);
					
					Person  person = new Person();
					person.setPartyId(party.getPartyId());
					person.setUserLoginId(agent.getUserLoginId());
					personMapper.insert(person);
					
					
					agent.setCreateTime(new Date());
					agent.setPartyId(party.getPartyId());
					agent.setAgentStatus("ACTIVITY");
					agentMapper.insert(agent);
					
				}catch (ParametersException e) {
					status.setRollbackOnly();
					throw e;
				}catch (Exception e) {
					status.setRollbackOnly();
					throw e;
				}
				
			}
		});
		
		
		
		
		
		
		
	}

	@Override
	public void modifyByPrimaryKey(Agent agent) {
		
		if(agent == null){
			throw new ParametersException("agent is null!");
		}
		agentMapper.delete(agent);
	}
	
	@Override
	public Agent loadByPrimaryKey(Long agentId) {
		
		if(agentId == null){
			throw new ParametersException("agentId is null!");
		}
		
		Agent agent = agentMapper.selectInfoById(agentId);
		return agent;
	}
	
	
	@Override
	public void deleteByPrimaryKey(Long companyId) {
		
				
	}
	
	@Override
	public PageModel<Agent> loadByQueryToPage(AgentQuery query) {
		
		
		List<Agent> datas = agentMapper.selectByQueryToPage(query);
		Integer total = agentMapper.countByQueryToPage(query);
		PageModel<Agent> pm = new PageModel<Agent>(total,datas);
		return pm;
	}

	@Override
	public List<Agent> getListByQuery(AgentQuery query) {
		
		List<Agent> agents  = agentMapper.selectListByQuery(query);
		
		return agents;
		
	}

	
}
